//联合类型和类型保护
interface Waiter15 {
  anjiao: boolean;
  say: () => {};
}
interface Teacher15 {
  anjiao: boolean;
  skill: () => {};
}
//联合类型,参数类型可以为联合类型中的一种
//四种联合类型的保护方式
//使用类型断言方式  as
function judgeWho15(animal: Waiter15 | Teacher15) {
  if (animal.anjiao) {
    (animal as Teacher15).skill();
  } else {
    (animal as Waiter15).say();
  }
}
//使用类型断言方式  in
function judgeWhoTwo15(animal: Waiter15 | Teacher15) {
  //如果animal有skill方法
  if ("skill" in animal) {
    animal.skill();
  } else {
    animal.say();
  }
}
//使用typeof进行类型保护
function add15(first: string | number, second: string | number) {
  if (typeof first === "string" || typeof second === "string") return `${first}${second}`;
  return first + second;
}

//使用instanceof进行类型保护
class NumberObj15 {
  count: number;
  constructor(count: number) {
    this.count = count;
  }
}
function addObj15(first: object | NumberObj15, second: object | NumberObj15) {
  // return first+second;
  //   return first.count + second.count;
  if (first instanceof NumberObj15 && second instanceof NumberObj15) return first.count + second.count;
  return 0;
}
const first = new NumberObj15(1);
const second = new NumberObj15(2);
console.log(addObj15(first, second));
